package com.tailyou.hms.neteasecloudmusic;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MusicAdapter extends BaseAdapter {

    private Context context;  // 上下文环境
    private ArrayList<MusicBean> listData; //列表要显示的数据

    public MusicAdapter(Context context, ArrayList<MusicBean> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public MusicBean getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder viewHolder = null;
//        if (viewHolder == null) {
//            viewHolder = new ViewHolder();
//            convertView = LayoutInflater.from(context).inflate(R.layout.layout_item_loved_music, null);
//            viewHolder.tvMusicName = convertView.findViewById(R.id.tvMusicName);
//            viewHolder.tvDesc = convertView.findViewById(R.id.tvDesc);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        viewHolder.tvMusicName.setText(getItem(position).getMusicName());
//        viewHolder.tvDesc.setText(getItem(position).getDesc());
        convertView = LayoutInflater.from(context).inflate(R.layout.layout_item_loved_music, null);
        TextView tvSeq = convertView.findViewById(R.id.tvSeq);
        TextView tvMusicName = convertView.findViewById(R.id.tvMusicName);
        TextView tvDesc = convertView.findViewById(R.id.tvDesc);
        tvSeq.setText("" + (getItem(position).getSqe() + 1));
        tvMusicName.setText(getItem(position).getMusicName());
        tvDesc.setText(getItem(position).getDesc());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MusicPlayActivity.class);
                intent.putExtra("MusicBean", getItem(position));
                context.startActivity(intent);
            }
        });
        return convertView;
    }

//    class ViewHolder {
//        private TextView tvMusicName;
//        private TextView tvDesc;
//    }

}
